﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Walson.Form
{
    /// <summary>
    /// 主飞行仪表显示面板
    /// </summary>
    [ToolboxItem(false)]
    public partial class PrimaryFlightDisplay : UserControl
    {
        /// <summary>
        /// 倾斜角度
        /// </summary>
        public double Bank
        {
            get
            {
                return pab.Bank;
            }
            set
            {
                pab.Bank = value;
            }
        }

        /// <summary>
        /// 俯仰角度
        /// </summary>
        public double Pitch
        {
            get
            {
                return pab.Pitch;
            }
            set
            {
                pab.Pitch = value;
            }
        }

        /// <summary>
        /// 海拔高度，单位为米
        /// </summary>
        public double Altitude
        {
            get
            {
                return nmiAltitude.Value;
            }
            set
            {
                nmiAltitude.Value = value;
                nmiAltitude.GroundAltitudeValue = this.Altitude - this.HeightFromGround;
            }
        }

        /// <summary>
        /// 空速
        /// </summary>
        public double AirSpeed
        {
            get
            {
                return nmiAirSpeed.Value;
            }
            set
            {
                nmiAirSpeed.Value = value;
            }
        }

        /// <summary>
        /// 最大侧倾限制
        /// </summary>
        public double BankLimit
        {
            get
            {
                return pab.BankLimit;
            }
            set
            {
                pab.BankLimit = value;
            }
        }

        /// <summary>
        /// 预设的海拔高度
        /// </summary>
        public double DestinationAltitude
        {
            get
            {
                return nmiAltitude.SetValue;
            }
            set
            {
                nmiAltitude.SetValue = value;
                lblDestinationAltitude.Text = nmiAltitude.SetValue.ToString("f0");
            }
        }

        /// <summary>
        /// 地面海拔高度
        /// </summary>
        private double GroundAltitude
        {
            get
            {
                return nmiAltitude.GroundAltitudeValue;
            }
            set
            {
                nmiAltitude.GroundAltitudeValue = value;
            }
        }

        /// <summary>
        /// 预设的空速
        /// </summary>
        public double DestinationSpeed
        {
            get
            {
                return nmiAirSpeed.SetValue;
            }
            set
            {
                if (value >= 0)
                {
                    nmiAirSpeed.SetValue = value;
                    lblDestinationAirSpeed.Text = nmiAirSpeed.SetValue.ToString("f0");
                }
            }
        }

        /// <summary>
        /// 航向
        /// </summary>
        public double Heading
        {
            get
            {
                return compass.Heading;
            }
            set
            {
                compass.Heading = value;
            }
        }

        /// <summary>
        /// 是否超速
        /// </summary>
        public bool OverSpeed
        {
            get
            {
                return pab.OverSpeed;
            }
            set
            {
                pab.OverSpeed = value;
            }
        }

        /// <summary>
        /// 是否失速
        /// </summary>
        public bool Stall
        {
            get
            {
                return pab.Stall;
            }
            set
            {
                pab.Stall = value;
            }
        }

        /// <summary>
        /// 垂直速度
        /// </summary>
        public double VerticalSpeed
        {
            get
            {
                return vsi.VerticalSpeed;
            }
            set
            {
                vsi.VerticalSpeed = value;
                lblPositiveVerticalSpeed.Text = vsi.VerticalSpeed.ToString("f2");
                lblNegtiveVerticalSpeed.Text = vsi.VerticalSpeed.ToString("f2");
                if (vsi.VerticalSpeed > 0.5)
                {
                    lblPositiveVerticalSpeed.Visible = true;
                    lblNegtiveVerticalSpeed.Visible = false;
                }
                else if (vsi.VerticalSpeed < -0.5)
                {
                    lblPositiveVerticalSpeed.Visible = false;
                    lblNegtiveVerticalSpeed.Visible = true;
                }
                else
                {
                    lblPositiveVerticalSpeed.Visible = false;
                    lblNegtiveVerticalSpeed.Visible = false;
                }
            }
        }

        /// <summary>
        /// 距离地面的高度。
        /// </summary>
        public double HeightFromGround
        {
            get
            {
                return pab.HeightFromGround;
            }
            set
            {
                pab.HeightFromGround = value;
                nmiAltitude.GroundAltitudeValue = this.Altitude - this.HeightFromGround;
                //GroundAltitude -= value / 100;
            }
        }

        /// <summary>
        /// 危险高度，低于该高度时PFD上将显示当前距地面的高度。
        /// </summary>
        public int CautionHeight
        {
            get
            {
                return pab.DangerHeight;
            }
            set
            {
                pab.DangerHeight = value;
            }
        }

        /// <summary>
        /// 主飞行数据显示器
        /// </summary>
        public PrimaryFlightDisplay()
        {
            InitializeComponent();

            this.AirSpeed = 0;
            this.Altitude = 0;
            this.Bank = 0;
            this.BankLimit = 45;
            this.DestinationAltitude = 0;
            this.DestinationSpeed = 0;
            this.GroundAltitude = 0;
            this.Heading = 0;
            this.OverSpeed = false;
            this.Pitch = 0;
            this.Stall = false;
            this.VerticalSpeed = 0;
        }

        /// <summary>
        /// 绘制控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            // 绘制圆角控件
            Rectangle rect = new Rectangle(0, 0, base.Width, base.Height);
            int radius = 50;
            System.Drawing.Drawing2D.GraphicsPath roundRect = new System.Drawing.Drawing2D.GraphicsPath();
            //顶端 
            roundRect.AddLine(rect.Left + radius - 1, rect.Top - 1, rect.Right - radius, rect.Top - 1);
            //右上角 
            roundRect.AddArc(rect.Right - radius, rect.Top - 1, radius, radius, 270, 90);
            //右边 
            roundRect.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius);
            //右下角
            roundRect.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
            //底边 
            roundRect.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom);
            //左下角 
            roundRect.AddArc(rect.Left - 1, rect.Bottom - radius, radius, radius, 90, 90);
            //左边 
            roundRect.AddLine(rect.Left - 1, rect.Top + radius, rect.Left - 1, rect.Bottom - radius);
            //左上角 
            roundRect.AddArc(rect.Left - 1, rect.Top - 1, radius, radius, 180, 90);
            this.Region = new Region(roundRect);
            base.OnPaint(e);
        }

        private void PrimaryFlightDisplay_SizeChanged(object sender, EventArgs e)
        {
            //panel5.Width = this.Width / 5;
            //panel9.Width = (int)(this.Width / 5.696);
            //panel1.Width = (int)(this.Width / 10.714);
        }
    }
}